package Work4.Slay_the_Maze.Map;

import java.util.Random;
import java.util.Scanner;
import Work4.Slay_the_Maze.Events.*;
import Work4.Slay_the_Maze.Player.*;

public class Maze {
	public Rooms[][] CreateMaze(int x,int y) {
		Random rd=new Random();
		Rooms[][] maze = new Rooms[x][y];
		boolean hasPath;
		do {
			for(int i=0;i<maze.length;i++) {
				for(int j=0;j<maze[i].length;j++) {
					maze[i][j] = new Rooms();
					maze[i][j].setAvailable(rd.nextBoolean());
					maze[i][j].setX(i);
					maze[i][j].setY(j);
					if(maze[i][j].isAvailable()) {
						maze[i][j].setMapIcon("□");
					}else {
						maze[i][j].setMapIcon("■");
					}
				}
			}
			
			hasPath=hasPathDFS(maze,0,0);
			
			for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    maze[i][j].setIsvisited(false);
                }
            } //将是否访问过重置为false
			
		}while(!hasPath); //生成一个有通路的迷宫
		
		for(int i=0;i<maze.length;i++) {
			for(int j=0;j<maze[i].length;j++) {
				if(maze[i][j].isAvailable()) {
					maze[i][j].setMapIcon("□");
				}
			}
		}
		
		for(int i=0;i<maze.length;i++) {
			for(int j=0;j<maze[i].length;j++) {
				if(maze[i][j].isAvailable()) {
					maze[i][j].setEvent(RandomEvent());
				}
			}
		}
		//生成事件
		
		return maze;
	} //生成迷宫
	
	public Events RandomEvent() {
		Random rd=new Random();
		EventsPool ep=new EventsPool();
		Events event;
		int temp=rd.nextInt(100)+1;
		if(temp>=1&&temp<=30) {
			event=ep.getEventspool().get(0);
		}else if(temp>30&&temp<=60) {
			event=ep.getEventspool().get(5);
		}else if(temp>60&&temp<=70) {
			event=ep.getEventspool().get(1);
		}else if(temp>70&&temp<=80) {
			event=ep.getEventspool().get(2);
		}else if(temp>80&&temp<=90){
			event=ep.getEventspool().get(3);
		}else {
			event=ep.getEventspool().get(4);
		}
		return event;
	}
	
	public void Themaze(Rooms[][] a) {
		for(int i=0;i<a.length;i++) {
			for(int j=0;j<a[i].length;j++) {
				if(a[i][j].isAvailable()) {
					System.out.printf(" "+a[i][j].getMapIcon());
				}else {
					System.out.printf(" "+a[i][j].getMapIcon());
				}
			}
			System.out.printf("\n");
		}
	} //打印迷宫
	
	 public boolean hasPathDFS(Rooms[][] maze,int x,int y) {
	        int N = maze.length, M = maze[0].length;
	        //越界或遇到墙或已访问
	        if (x<0||x>=N||y<0||y>=M||!maze[x][y].isAvailable()||maze[x][y].isIsvisited()){
	            return false;
	        }
	        //终点判断
	        if (x==N-1&&y==M-1){
	            return true;
	        }
	        //标记为已访问
	        maze[x][y].setIsvisited(true);
	        //尝试四个方向
	        if (hasPathDFS(maze,x+1,y) ||  // 下
	            hasPathDFS(maze,x,y+1) ||  // 右
	            hasPathDFS(maze,x-1,y) ||  // 上
	            hasPathDFS(maze,x,y-1)) {  // 左
	            return true;
	        }
	        maze[x][y].setIsvisited(false);
	        return false;
	    }//迷宫是否有出口
	
	public int BeginGame() {
		Player player=new Player();
		Scanner sc=new Scanner(System.in);
		System.out.println("走迷宫");
		System.out.println("数字'P'代表当前位置，输入wasd进行移动");
		System.out.println("迷宫左上角为入口，右下角为出口");
		System.out.println("点击任意键开始游戏");
		int x0=8;
		int	y0=8;
		Rooms[][] maze;
		maze=CreateMaze(x0,y0);
		maze[0][0].setMapIcon("P");
		maze[0][0].setIsvisited(true);
		int x=0,y=0;
		System.out.println("-----迷宫-----");
		Themaze(maze);
		while((maze[maze.length-1][maze[0].length-1].getMapIcon()!="P")&&(player.getHp()>0)) {
			System.out.println("输入wasd进行移动,查看背包输入b,若要结束游戏则输入no");
			String action = sc.next();
			switch(action) {
				case"b":
					player.mybag();
					System.out.println("-----迷宫-----");
					Themaze(maze);
					player.Printplayerinfo();
					break;
				case "w":
					if(x==0||!maze[x-1][y].isAvailable()) {
						System.out.println("无法向上移动，请再次输入");
						break;
					}
					maze[x][y].setMapIcon("□");
					maze[x-1][y].setMapIcon("P");
					if(!maze[x-1][y].isIsvisited()) {
						maze[x-1][y].getEvent().doit(player);
						maze[x-1][y].setIsvisited(true);
					}
					x=x-1;
					System.out.println("-----迷宫-----");
					Themaze(maze);
					player.Printplayerinfo();
					break;
				case "a":
					if(y==0||!maze[x][y-1].isAvailable()) {
						System.out.println("无法向左移动，请再次输入");
						break;
					}
					maze[x][y].setMapIcon("□");
					maze[x][y-1].setMapIcon("P");
					if(!maze[x][y-1].isIsvisited()) {
						maze[x][y-1].getEvent().doit(player);
						maze[x][y-1].setIsvisited(true);
					}
					y=y-1;
					System.out.println("-----迷宫-----");
					Themaze(maze);
					player.Printplayerinfo();
					break;
				case "s":
					if(x==maze.length-1||!maze[x+1][y].isAvailable()) {
						System.out.println("无法向下移动，请再次输入");
						break;
					}
					maze[x][y].setMapIcon("□");
					maze[x+1][y].setMapIcon("P");
					if(!maze[x+1][y].isIsvisited()) {
						maze[x+1][y].getEvent().doit(player);
						maze[x+1][y].setIsvisited(true);
					}
					x=x+1;
					System.out.println("-----迷宫-----");
					Themaze(maze);
					player.Printplayerinfo();
					break;
				case "d":
					if(y==maze[maze.length-1].length-1||!maze[x][y+1].isAvailable()) {
						System.out.println("无法向右移动，请再次输入");
						break;
					}
					maze[x][y].setMapIcon("□");
					maze[x][y+1].setMapIcon("P");
					if(!maze[x][y+1].isIsvisited()) {
						maze[x][y+1].getEvent().doit(player);
						maze[x][y+1].setIsvisited(true);
					}
					y=y+1;
					System.out.println("-----迷宫-----");
					Themaze(maze);
					player.Printplayerinfo();
					break;
				case "no":
					maze[maze.length-1][maze[maze.length-1].length-1].setMapIcon("P");
					System.out.println("游戏结束");
					return 0;
				default:
					System.out.println("输入错误，请再次输入");
			}
		}
		if(player.getHp()<=0) {
			System.out.println("失败！");
			return 0;
		}
		System.out.println("成功！");
		return 0;
	} //游戏逻辑
}
